package com.jxzyk.model.course;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.ui.fragment.BaseFragment;
import com.core.util.T;
import com.jxzyk.R;
import com.jxzyk.adapter.OutlineAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.entity.tree.Material;
import com.jxzyk.entity.tree.Outline;
import com.jxzyk.entity.tree.Outlines;
import com.jxzyk.entity.tree.Tree;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.CourseRequest;
import com.jxzyk.view.tree.FileBean;
import com.jxzyk.view.tree.SimpleTreeAdapter;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhy.tree.bean.Node;
import com.zhy.tree.bean.TreeListViewAdapter;
import com.zhy.tree.bean.TreeListViewAdapter.OnTreeNodeClickListener;

public class OutlineFragment extends BaseFragment {
	private List<FileBean> mDatas = new ArrayList<FileBean>();
	@ViewInject(R.id.id_tree)
	private ListView mTree;
	private TreeListViewAdapter mAdapter;
	private Course course;
	private List<I_List> lists;
	private CourseActivity activity;

	@Override
	public void initData(Bundle bundle) {
		activity = (CourseActivity) getActivity();
		bundle = getActivity().getIntent().getExtras();
		if (bundle != null) {
			course = (Course) bundle.getSerializable(Course.class.getName());
		}
	}

	@Override
	public int initUiRes() {
		return R.layout.fra_outline;
	}

	@Override
	public void initUi(View view) {

		initDatas();
	}

	public void loadMaterials(int key, List<I_List> materials) {
		// TODO 将素材加入到map集合中
		if (materials != null) {
			activity.vaperMap.put(key, materials);
		}
	}

	private void initDatas() {
		lists = new ArrayList<>();
		CourseRequest request = new CourseRequest(getContext(),
				API.Course.getOutline);
		request.setParams(course.getCourse_id());
		activity.vaperMap.clear();
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				JSONArray array = json.getJSONArray("data");
				for (int i = 0; i < array.size(); i++) {
					json = array.getJSONObject(i);
					Tree tree = new Tree(json);
					lists.add(tree);
				}
				// T.toast(((Tree) lists.get(0)).getOutlinetitle() + "");
				int x = 0;
				int pid = 0;
				for (int i = 0; i < lists.size(); i++) {
					Tree tree = (Tree) lists.get(i);
					mDatas.add(new FileBean(++x, 0, tree.getOutlinetitle()));// 根目录0
					List<Outlines> childrens = tree.getChildren();
					pid = x;
					for (int j = 0; j < childrens.size(); j++) {
						Outlines outlines = childrens.get(j);
						mDatas.add(new FileBean(++x, pid, outlines
								.getOutlinetitle()));// 二级目录
						List<Outline> outs = outlines.getChildren();
						List<I_List> materials = outlines.getMaterial();
						loadMaterials(x, materials);
						int pid_2 = x;// 二级pid
						for (int k = 0; k < outs.size(); k++) {
							Outline outline = outs.get(k);
							mDatas.add(new FileBean(++x, pid_2, outline
									.getOutlinetitle()));// 三级目录
							materials = outline.getMaterial();
							loadMaterials(x, materials);
						}
					}
				}
				// mDatas.add(new FileBean(2, 1, "认识机械"));
				// mDatas.add(new FileBean(3, 1, "了解机械设计"));
				try {
					mAdapter = new SimpleTreeAdapter<FileBean>(mTree,
							getContext(), mDatas, 10);
					mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener() {
						@Override
						public void onClick(Node node, int position) {
							// T.toast(node.getName() + "|" + position + "|id:"
							// + node.getId());
							activity.initViewpager(node, node.getId());
						}
					});
					mTree.setAdapter(mAdapter);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {

			}
		});
		request.show();
	}

}
